/*

    This file is part of Blue Violin.

    Blue Violin is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Blue Violin is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero Public License for more details.

    You should have received a copy of the GNU Affero Public License
    along with Blue Violin.  If not, see <http://www.gnu.org/licenses/>.

    Blue Violin  Copyright (C) 2008  Blue Violin, Inc. & Josh Watts <josh.watts@blueviolin.org>
	For more information, visit http://www.blueviolin.org/

    This program comes with ABSOLUTELY NO WARRANTY.

*/

function EditProfile()
{
	this.m_oPrevious = null;
}

EditProfile.prototype.select = function()
{
	var oElement = window.event.srcElement;
	var oHttp = null;
	var sProfile = "";
	var oProfile = null;
	
	if (null != this.m_oPrevious)
	{
		this.m_oPrevious.style.backgroundColor = "white";
		this.m_oPrevious.style.color = "black";
	}
	
	if ("IMG" == oElement.tagName.toUpperCase())
	{
		oElement = oElement.parentNode;
	}
	
	oElement.style.backgroundColor = "#01457a";
	oElement.style.color = "white";
	
	this.m_oPrevious = oElement;

	oHttp = $http("POST", "../scripts.php");
	oHttp.send("action=openprofile&guid=" + oElement.id);
	sProfile = oHttp.responseText;
	
	oProfile = sProfile.parseJSON();
	
	$get("txtProfileName").value = oProfile.name;
	$get("txtDefaultUrl").value = oProfile.url;
	
	$get("checkEmailOnlyOnFail").checked = (1 == oProfile.emailOnlyOnFail) ? true : false;
};


EditProfile.prototype.save = function()
{
	if (null != this.m_oPrevious)
	{
		window.dialogArguments.guid = this.m_oPrevious.id;
		window.dialogArguments.name = $get("txtProfileName").value;
		window.dialogArguments.defaultUrl = $get("txtDefaultUrl").value;
		window.dialogArguments.emailOnlyOnFail = $get("checkEmailOnlyOnFail").checked;
	}
	window.close();
};


EditProfile.prototype.selectAndOpen = function()
{
	var oElement = window.event.srcElement;
	if ("IMG" == oElement.tagName.toUpperCase())
	{
		oElement = oElement.parentNode;
	}
	var sProfileName = oElement.childNodes.item(1).data.trim();
	window.dialogArguments.name = sProfileName;
	window.close();
};

	
EditProfile.prototype.cancel = function()
{
	window.dialogArguments.name = null;
	window.close();
};

	
EditProfile.prototype.keyPress = function()
{
	if (27 == window.event.keyCode)
	{
		this.cancel();
	}
};

	

var oEditProfile = new EditProfile();


function EditProfileBodyLoad()
{
	var divProfileList = $get("divProfileList");
	var nChildren = divProfileList.childNodes.length;
	var oNode = null;
	var i = 0;
	
	for (i = 0;
		 i < nChildren;
		 ++i)
	{
		oNode = divProfileList.childNodes[i];
		oNode.attachEvent("onclick",
						  $createDelegate(oEditProfile, oEditProfile.select));
		oNode.attachEvent("ondblclick",
						  $createDelegate(oEditProfile, oEditProfile.selectAndOpen));
	}


	document.body.attachEvent("onkeypress",
							  $createDelegate(oEditProfile, oEditProfile.keyPress));
	
	$get("btnSave").attachEvent("onclick",
								$createDelegate(oEditProfile, oEditProfile.save));
	$get("btnCancel").attachEvent("onclick",
								  $createDelegate(oEditProfile, oEditProfile.cancel));

}
